package sync_bank_test2;

public class Bank {
    private final double[] accounts;
    public Bank(int n, double initialBalance) {
	accounts = new double[n];
	for (int i = 0; i < n; i++)
	    accounts[i] = initialBalance;
    }

    public synchronized void transfer(int from, int to, double amount) {
	while (accounts[from] < amount)
		try {
			wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	System.out.print(Thread.currentThread());
	accounts[from] -= amount;
	System.out.printf(" %10.2f from %d to %d", amount, from, to);
	accounts[to] += amount;
	System.out.printf(" Total Balance: %10.2f%n", getTotalBanlance());
	notifyAll();
    }
    
    public int size() {
    	return accounts.length;
    }

    public synchronized double getTotalBanlance() {
	double sum = 0;
	for (double d : accounts)
	    sum += d;
	return sum;
    }
}